$(function(){
	var cookieName = $.cookie('username')
	if(cookieName){
//		onerror当文件加载失败时会触发这个事件
		var header = '<img src="uploads/' + $.cookie('header') + '" onerror="this.src=\'images/user.png\'">'
		$('#user').empty().html(header+cookieName)
	}
	 $('.dropdown-menu li').last().click(function(){
    	$.removeCookie('username')
    	location.reload()
    })
	 $('#user').click(function(){
	 	if(!cookieName){
	 		$(this).removeAttr('data-toggle')
	 		location.href = '/signin'
	 	}
	 })
	 
	  $('#ask').click(function(){
	 	if(cookieName){
	 		location.href="/ask"
	 	}
	 })
	 
	function checkNumber(){
		var $val = $('textarea').val().length
		$('textarea').next().text(140-$val + '/140')
	}
	
	var timer
	$('textarea').focus(function(){
		timer = setInterval(checkNumber,20)
	})
	$('textarea').blur(function(){
		clearInterval(timer)
	})
	
	
	$('.modal-close').click(function(){
		$('.modal').modal('hide')
	})
	
	
		$('form').submit(function(ev){
			ev.preventDefault()
			var data = $(this).serialize()
			$.post('/ask',data,function(data){
				$('.modal-body p').text(data.message)
				$('.modal').modal('show')
				if(data.code = 'success'){
					$('.modal').on('hidden.bs.modal',function(){
						location.href = '/'
					})
			}
		})
	})
})
